<?php

namespace App\Kuafu\Shop\Resources\B;

use App\Kuafu\Shop\Enum\ShopCodeEnum;
use App\Kuafu\Shop\Model\Shop;
use App\Kuafu\Shop\Service\ShopService;
use App\Kuafu\WaiMai\Enum\WaiMaiDBEnum;
use App\Kuafu\WaiMai\Service\WaiMaiShopService;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class ShopListResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param Request $request
     * @return array
     */
    public function toArray($request): array
    {
        $shopBusinessTimeChangeByDb = ShopService::getShopBusinessTimeChangeByDb($this->startAt, $this->endAt);

        return [
            'id'                       => $this->id,
            'shopCode'                 => str_pad($this->shopCode, ShopCodeEnum::CODE_TEXT[ShopCodeEnum::LEADING_ZERO],
                "0", STR_PAD_LEFT),
            'name'                     => $this->name,
            'shortName'                => $this->shortName,
            'shopMobile'               => $this->shopMobile,
            'address'                  => $this->address,
            'status'                   => $this->status,
            'updatedAt'                => $this->updatedAt,
            'createdAt'                => $this->createdAt,
            'startAt'                  => $shopBusinessTimeChangeByDb['startAt'],
            'endAt'                    => $shopBusinessTimeChangeByDb['endAt'],
            'createdBy'                => $this->createdByUidRel ? $this->createdByUidRel->username : '',
            'updatedBy'                => $this->updatedByUidRel ? $this->updatedByUidRel->username : '',
            'bind'                     => $this->getBindInfo(),
            'payments'                 => $this->whenLoaded('shopFuYouPaymentsRel') ?
                ShopPaymentsResource::collection([$this->shopFuYouPaymentsRel])
                                    ->getIterator()
                                    ->current() : null,
            'invoice'                  => json_decode($this->invoice, 1),
            'dyCouponVerifySwitch'     => $this->dyCouponVerifySwitch,
            'dyCouponVerifySwitchText' => Shop::DY_COUPON_VERIFY_SWITCH_TEXT[$this->dyCouponVerifySwitch] ?? ""
        ];
    }

    /**
     * @return array|array[]
     */
    public function getBindInfo(): array
    {
        $bind = [
            'meituanWaimai'  => [
                'bound'      => false,
                'bindUrl'    => '',
                'unbindUrl'  => '',
                'shopName'   => '',
                'sourceType' => WaiMaiDBEnum::SOURCE_TYPE_MT_WM,
            ],
            'elemeWaimai'    => [
                'bound'      => false,
                'bindUrl'    => '',
                'unbindUrl'  => '',
                'shopName'   => '',
                'sourceType' => WaiMaiDBEnum::SOURCE_TYPE_ELM,
            ],
            'meituanTuangou' => [
                'bound'      => false,
                'bindUrl'    => '',
                'unbindUrl'  => '',
                'shopName'   => '',
                'sourceType' => WaiMaiDBEnum::SOURCE_TYPE_MT_TG,
            ],
            'douyin'         => [
                'bound'      => false,
                'bindUrl'    => '',
                'unbindUrl'  => '',
                'shopName'   => '',
                'sourceType' => WaiMaiDBEnum::SOURCE_TYPE_DOUYIN,
            ],
            'xhs'            => [
                'bound'      => false,
                'bindUrl'    => '',
                'unbindUrl'  => '',
                'shopName'   => '',
                'sourceType' => WaiMaiDBEnum::SOURCE_TYPE_XHS,
            ],
        ];

        foreach ($this->waimaiShopMappings as $waimaiShopMapping) {
            switch ($waimaiShopMapping->sourceType) {
                case WaiMaiDBEnum::SOURCE_TYPE_MT_WM:
                    $bind['meituanWaimai']['bound']    = true;
                    $bind['meituanWaimai']['shopName'] = $waimaiShopMapping->thirdShopName;
                    break;
                case WaiMaiDBEnum::SOURCE_TYPE_ELM:
                    $bind['elemeWaimai']['bound']    = true;
                    $bind['elemeWaimai']['shopName'] = $waimaiShopMapping->thirdShopName;
                    break;
                case WaiMaiDBEnum::SOURCE_TYPE_MT_TG:
                    $bind['meituanTuangou']['bound']    = true;
                    $bind['meituanTuangou']['shopName'] = $waimaiShopMapping->thirdShopName;
                    break;
                case WaiMaiDBEnum::SOURCE_TYPE_DOUYIN:
                    $bind['douyin']['bound']    = true;
                    $bind['douyin']['shopName'] = $waimaiShopMapping->thirdShopName;
                    break;
                case WaiMaiDBEnum::SOURCE_TYPE_XHS:
                    $bind['xhs']['bound']    = true;
                    $bind['xhs']['shopName'] = $waimaiShopMapping->thirdShopName;
                    break;
            }
        }

        // 补充网址
        foreach ($bind as $kk => $vv) {
            if ($vv['bound']) {
                $bind[$kk]['unbindUrl'] = (new WaiMaiShopService($this->resource))->unbindUrl($vv['sourceType']);
            } else {
                $bind[$kk]['bindUrl'] = (new WaiMaiShopService($this->resource))->bindUrl($vv['sourceType']);
            }
        }

        return $bind;
    }
}
